package cn.pjz.dataUpload.service.impl;

import cn.pjz.dataUpload.entity.RTransaction;
import cn.pjz.dataUpload.mapper.RTransactionMapper;
import cn.pjz.dataUpload.service.RTransactionService;
import cn.pjz.dataUpload.web.vo.RTransactionVo;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.Year;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IRTransactionServiceImpl extends ServiceImpl<RTransactionMapper, RTransaction> implements RTransactionService {

    @Autowired
    private RTransactionMapper rTransactionMapper;

    @Override
    public void insertBy(RTransaction item) {
        rTransactionMapper.insertBy(item);
    }

    @Override
    public Map<String, String> module_1() {
        Map<String, String> map = new HashMap<>();
        // 本年花费
        String year = Year.now().getValue() + "";
        String mappers = rTransactionMapper.yearByMoney(year,1);
        map.put("mappers",mappers);

        // 昨日花费
        LocalDate today = LocalDate.now();
        LocalDate yesterday = today.minusDays(1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String formattedYesterday = yesterday.format(formatter);
        String yesterdayExpense = rTransactionMapper.getYesterdayExpense(formattedYesterday,1);
        yesterdayExpense = yesterdayExpense == null ?
                "0" :
                yesterdayExpense;
        map.put("yesterdayExpense",yesterdayExpense);

        // 上月花费
        java.time.YearMonth currentYearMonth = java.time.YearMonth.now();
        java.time.YearMonth previousYearMonth = currentYearMonth.minusMonths(1);
        LocalDate previousMonthFirstDay = previousYearMonth.atDay(1);
        String format = previousMonthFirstDay.format(formatter);
        String lastMonthExpenditure = rTransactionMapper.getLastMonthExpenditure(format,1);
        map.put("lastMonthExpenditure",lastMonthExpenditure);

        LocalDate today1 = LocalDate.now();
        LocalDate yesterday1 = today1.minusDays(1);
        LocalDate previousMonthYesterday = yesterday1.minusMonths(1);
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyyMMdd");
        String formattedDate = previousMonthYesterday.format(formatter1);
        String yesterdayExpense_1 = rTransactionMapper.getYesterdayExpense(formattedDate,1);
        // 环比增长率 = (本期数 - 上期数) ÷ 上期数 × 100%。
        if (yesterdayExpense_1 == null) {
            map.put("yesterdaySpending","100%");
        }else {
            map.put("yesterdaySpending", ((Double.parseDouble(yesterdayExpense) - Double.parseDouble(yesterdayExpense_1)) / Double.parseDouble(yesterdayExpense_1) * 100) + "%");
        }
        return map;
    }

    @Override
    public List<RTransaction> module_2() {
        return rTransactionMapper.getCountQuantityGoods();
    }

    @Override
    public  HashMap<String,Object> module_3(RTransactionVo rTransactionVo) {
        HashMap<String, Object> map = new HashMap<>();
        Integer current = (rTransactionVo.getCurrent() - 1) * 10;
        List<RTransaction> rTransactions = rTransactionMapper.getSelectList(current,rTransactionVo.getTransactionDate());
        int num = rTransactionMapper.getSelectListNum();
        map.put("data",rTransactions);
        map.put("num",num);
        return map;
    }
}
